<template>
  <div class="dashboard">
    <!-- 侧边栏 -->
    <aside class="sidebar">
      <div class="logo">
        <h2>🛡️ 管理系统</h2>
      </div>
      
      <nav class="menu">
        <router-link to="/dashboard" class="menu-item" active-class="active">
          <i class="material-icons">dashboard</i>
          <span>仪表板</span>
        </router-link>
        <router-link to="/businesses" class="menu-item" active-class="active">
          <i class="material-icons">store</i>
          <span>商户管理</span>
        </router-link>
        <router-link to="/users" class="menu-item" active-class="active">
          <i class="material-icons">people</i>
          <span>用户管理</span>
        </router-link>
        <router-link to="/orders" class="menu-item" active-class="active">
          <i class="material-icons">assignment</i>
          <span>订单管理</span>
        </router-link>
        <router-link to="/coupons" class="menu-item" active-class="active">
          <i class="material-icons">card_giftcard</i>
          <span>优惠券管理</span>
        </router-link>
        <router-link to="/levels" class="menu-item" active-class="active">
          <i class="material-icons">military_tech</i>
          <span>等级管理</span>
        </router-link>
        <router-link to="/statistics" class="menu-item" active-class="active">
          <i class="material-icons">bar_chart</i>
          <span>数据统计</span>
        </router-link>
      </nav>
      
      <div class="user-info">
        <p>{{ adminName }}</p>
        <button @click="handleLogout" class="logout-btn">
          <i class="material-icons">logout</i>
          退出登录
        </button>
      </div>
    </aside>
    
    <!-- 主内容区 -->
    <main class="main-content">
      <header class="top-bar">
        <h1>仪表板</h1>
        <div class="user-menu">
          <span>{{ adminName }}</span>
          <i class="material-icons">account_circle</i>
        </div>
      </header>
      
      <div class="content">
        <!-- 主要统计数据 -->
        <div class="stats-grid">
          <div class="stat-card primary">
            <i class="material-icons">people</i>
            <div class="stat-info">
              <p class="stat-label">用户总数</p>
              <p class="stat-value">{{ formatNumber(stats.totalUsers) }}</p>
              <p class="stat-detail">活跃: {{ formatNumber(stats.activeUsers) }} | 禁用: {{ formatNumber(stats.inactiveUsers) }}</p>
            </div>
          </div>
          
          <div class="stat-card success">
            <i class="material-icons">store</i>
            <div class="stat-info">
              <p class="stat-label">商户总数</p>
              <p class="stat-value">{{ formatNumber(stats.totalBusinesses) }}</p>
              <p class="stat-detail">活跃: {{ formatNumber(stats.activeBusinesses) }}</p>
            </div>
          </div>
          
          <div class="stat-card warning">
            <i class="material-icons">shopping_cart</i>
            <div class="stat-info">
              <p class="stat-label">订单总数</p>
              <p class="stat-value">{{ formatNumber(stats.totalOrders) }}</p>
              <p class="stat-detail">今日: {{ formatNumber(stats.todayOrders) }} | 本月: {{ formatNumber(stats.monthlyOrders) }}</p>
            </div>
          </div>
          
          <div class="stat-card info">
            <i class="material-icons">attach_money</i>
            <div class="stat-info">
              <p class="stat-label">平台收入</p>
              <p class="stat-value">{{ formatCurrency(stats.totalRevenue) }}</p>
              <p class="stat-detail">今日: {{ formatCurrency(stats.todayRevenue) }} | 本月: {{ formatCurrency(stats.monthlyRevenue) }}</p>
            </div>
          </div>
        </div>

        <!-- 详细统计数据 -->
        <div class="detail-stats">
          <div class="detail-card">
            <h3>订单分析</h3>
            <div class="detail-grid">
              <div class="detail-item">
                <span class="detail-label">平均订单金额</span>
                <span class="detail-value">{{ formatCurrency(stats.avgOrderAmount) }}</span>
              </div>
              <div class="detail-item">
                <span class="detail-label">今日订单</span>
                <span class="detail-value">{{ formatNumber(stats.todayOrders) }}</span>
              </div>
              <div class="detail-item">
                <span class="detail-label">本月订单</span>
                <span class="detail-value">{{ formatNumber(stats.monthlyOrders) }}</span>
              </div>
            </div>
          </div>

          <div class="detail-card">
            <h3>收入分析</h3>
            <div class="detail-grid">
              <div class="detail-item">
                <span class="detail-label">总收入</span>
                <span class="detail-value">{{ formatCurrency(stats.totalRevenue) }}</span>
              </div>
              <div class="detail-item">
                <span class="detail-label">今日收入</span>
                <span class="detail-value">{{ formatCurrency(stats.todayRevenue) }}</span>
              </div>
              <div class="detail-item">
                <span class="detail-label">本月收入</span>
                <span class="detail-value">{{ formatCurrency(stats.monthlyRevenue) }}</span>
              </div>
            </div>
          </div>

          <div class="detail-card">
            <h3>用户分析</h3>
            <div class="detail-grid">
              <div class="detail-item">
                <span class="detail-label">总用户数</span>
                <span class="detail-value">{{ formatNumber(stats.totalUsers) }}</span>
              </div>
              <div class="detail-item">
                <span class="detail-label">活跃用户</span>
                <span class="detail-value">{{ formatNumber(stats.activeUsers) }}</span>
              </div>
              <div class="detail-item">
                <span class="detail-label">用户活跃率</span>
                <span class="detail-value">{{ calculateActiveRate(stats.activeUsers, stats.totalUsers) }}%</span>
              </div>
            </div>
          </div>
        </div>
        
        <div class="quick-actions">
          <h2>快速操作</h2>
          <div class="action-buttons">
            <router-link to="/businesses" class="action-btn">
              <i class="material-icons">add</i>
              添加商户
            </router-link>
            <router-link to="/users" class="action-btn">
              <i class="material-icons">person_add</i>
              添加用户
            </router-link>
            <router-link to="/coupons" class="action-btn">
              <i class="material-icons">edit</i>
              管理优惠券
            </router-link>
            <router-link to="/levels" class="action-btn">
              <i class="material-icons">settings</i>
              调整等级
            </router-link>
          </div>
        </div>
      </div>
    </main>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  name: 'Dashboard',
  data() {
    return {
      stats: {
        businessCount: 0,
        userCount: 0,
        couponCount: 0,
        totalRevenue: 0
      }
    }
  },
  computed: {
    adminName() {
      return this.$store.state.adminName || '管理员'
    }
  },
  mounted() {
    this.fetchDashboardStats()
  },
  methods: {
    async fetchDashboardStats() {
      try {
        const response = await axios.post('http://localhost:8888/elm/admin/dashboard/stats')
        if (response.data.code === 200 || response.data.code === '200') {
          this.stats = response.data.result || {}
        }
      } catch (error) {
        console.error('获取统计数据失败:', error)
        // 使用默认值
        this.stats = {
          totalBusinesses: 0,
          totalUsers: 0,
          totalOrders: 0,
          totalRevenue: '0.00'
        }
      }
    },
    handleLogout() {
      this.$store.dispatch('logout')
      this.$router.push('/login')
    },
    
    // 格式化数字
    formatNumber(num) {
      if (num === null || num === undefined) return '0'
      return parseInt(num || 0).toLocaleString('zh-CN')
    },
    
    // 格式化货币
    formatCurrency(amount) {
      if (amount === null || amount === undefined) return '¥0.00'
      const num = parseFloat(amount || 0)
      return '¥' + num.toLocaleString('zh-CN', {
        minimumFractionDigits: 2,
        maximumFractionDigits: 2
      })
    },
    
    // 计算活跃率
    calculateActiveRate(active, total) {
      if (!total || total === 0) return '0'
      return ((active / total) * 100).toFixed(1)
    }
  }
}
</script>

<style scoped>
.dashboard {
  display: flex;
  min-height: 100vh;
  background-color: #f5f5f5;
}

.sidebar {
  width: 250px;
  background: linear-gradient(135deg, #FF6B6B 0%, #FF5252 100%);
  color: white;
  display: flex;
  flex-direction: column;
  box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1);
}

.logo {
  padding: 20px;
  text-align: center;
  border-bottom: 1px solid rgba(255, 255, 255, 0.2);
}

.logo h2 {
  font-size: 20px;
  margin: 0;
}

.menu {
  flex: 1;
  padding: 20px 0;
}

.menu-item {
  display: flex;
  align-items: center;
  padding: 15px 20px;
  color: rgba(255, 255, 255, 0.8);
  text-decoration: none;
  transition: all 0.3s;
  border-left: 3px solid transparent;
}

.menu-item:hover {
  background-color: rgba(255, 255, 255, 0.1);
  color: white;
}

.menu-item.active {
  background-color: rgba(255, 255, 255, 0.2);
  color: white;
  border-left-color: white;
}

.menu-item i {
  margin-right: 12px;
}

.user-info {
  padding: 20px;
  border-top: 1px solid rgba(255, 255, 255, 0.2);
}

.user-info p {
  margin: 0 0 10px 0;
  font-size: 14px;
}

.logout-btn {
  width: 100%;
  padding: 8px;
  background-color: rgba(255, 255, 255, 0.2);
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 5px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  transition: all 0.3s;
}

.logout-btn:hover {
  background-color: rgba(255, 255, 255, 0.3);
}

.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.top-bar {
  background: white;
  padding: 20px 30px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

.top-bar h1 {
  margin: 0;
  font-size: 24px;
  color: #333;
}

.user-menu {
  display: flex;
  align-items: center;
  gap: 10px;
  color: #666;
}

.content {
  flex: 1;
  overflow-y: auto;
  padding: 30px;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 20px;
  margin-bottom: 30px;
}

.stat-card {
  background: white;
  border-radius: 10px;
  padding: 20px;
  display: flex;
  align-items: center;
  gap: 15px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  transition: transform 0.3s;
}

.stat-card:hover {
  transform: translateY(-5px);
}

.stat-card i {
  font-size: 32px;
  color: #FF6B6B;
}

.stat-info {
  flex: 1;
}

.stat-label {
  margin: 0;
  color: #999;
  font-size: 12px;
}

.stat-value {
  margin: 5px 0 0 0;
  font-size: 24px;
  font-weight: 600;
  color: #333;
}

.quick-actions {
  background: white;
  border-radius: 10px;
  padding: 20px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

.quick-actions h2 {
  margin: 0 0 15px 0;
  font-size: 16px;
  color: #333;
}

.action-buttons {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 15px;
}

.action-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 15px;
  background: linear-gradient(135deg, #FF6B6B 0%, #FF5252 100%);
  color: white;
  border-radius: 8px;
  text-decoration: none;
  transition: transform 0.3s;
  gap: 8px;
}

.action-btn:hover {
  transform: translateY(-3px);
}

.action-btn i {
  font-size: 24px;
}

/* 新增样式 */
.stat-card.primary {
  border-left: 4px solid #2196F3;
}

.stat-card.success {
  border-left: 4px solid #4CAF50;
}

.stat-card.warning {
  border-left: 4px solid #FF9800;
}

.stat-card.info {
  border-left: 4px solid #9C27B0;
}

.stat-detail {
  font-size: 12px;
  color: #666;
  margin: 5px 0 0 0;
}

.detail-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 20px;
  margin-bottom: 30px;
}

.detail-card {
  background: white;
  border-radius: 10px;
  padding: 20px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

.detail-card h3 {
  margin: 0 0 15px 0;
  font-size: 16px;
  color: #333;
  border-bottom: 2px solid #f0f0f0;
  padding-bottom: 10px;
}

.detail-grid {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.detail-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
}

.detail-label {
  font-size: 14px;
  color: #666;
}

.detail-value {
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .stats-grid {
    grid-template-columns: 1fr;
  }
  
  .detail-stats {
    grid-template-columns: 1fr;
  }
  
  .stat-card {
    padding: 15px;
  }
  
  .stat-value {
    font-size: 20px;
  }
}
</style>
